//
//  PageTitleView.swift
//  WhyTest
//
//  Created by 宋广越 on 2022/2/11.
//

import UIKit
private let kScrollLineH : CGFloat = 2
//自定义一个代理方法
protocol PageTitleViewDelegate : AnyObject {
    func pageTitleVeiw(titleView : PageTitleView, selectedIndex index : Int)
}
//定义常量
private let kNormalColor : (CGFloat, CGFloat, CGFloat) = (85,85,85)
private let kSelectColor : (CGFloat, CGFloat, CGFloat) = (255,128,0)

class PageTitleView: UIView {
     //设置一个代理属性或者成员变量
    weak var delegate : PageTitleViewDelegate?
    //定义属性
    private var currentIndex : Int = 0
    private var titles : [String]
    
    //懒加载属性
    private lazy var scrollView : UIScrollView = {
       let scrollView = UIScrollView()
        scrollView.showsHorizontalScrollIndicator = false
        scrollView.scrollsToTop = false
        scrollView.isPagingEnabled = false
        scrollView.bounces = false
        return scrollView
    }()
    //设置移动的线
    
    //自定义一个构造函数
    init(frame:CGRect,titles:[String]){
        self.titles = titles
        super.init(frame: frame)
        setupUI()
    }
    //设置ui界面
    private lazy var scrollLine : UIView = {
        //设置scrollline
        let scrollLine = UIView()
        scrollLine.backgroundColor = UIColor.orange
        return scrollLine
    }()
//增加懒加载属性
    private lazy var titleLables : [UILabel] = [UILabel]()
    
    
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
 
    
}
    // 设置ui界面
     extension PageTitleView {
         //确定labled的唯一值
         
         private func setupUI(){
             
            
             //添加scrollview
             addSubview(scrollView)
             scrollView.frame = bounds
             
             //添加title 对应的lable
             setupTitleLables()
             
             //设置底线和滚动的滑块
             setupBottomMenuAndScrollLine()
             
             
         }
         private func setupTitleLables(){
             for (index ,title) in titles.enumerated(){
                  //创建uilable
                 let lable = UILabel()
                 //设置属性
                 lable.text = title
                 lable.tag = index
                 lable.font = UIFont.systemFont(ofSize: 16.0)
                 lable.textColor = UIColor(r: kNormalColor.0, g: kNormalColor.1, b: kNormalColor.2)
                 lable.textAlignment = .center
                 
                 //设置frame
                 let  lableW : CGFloat = frame.width / CGFloat(titles.count)
                 let  labelH : CGFloat = frame.height - kScrollLineH
                 let  lableX : CGFloat = lableW*CGFloat(index)
                 let  lableY : CGFloat = 0
                 lable.frame = CGRect(x: lableX, y: lableY, width: lableW, height: labelH)
                 
                 //将lable 添加到scrollView
                 scrollView.addSubview(lable)
                 titleLables.append(lable)
                 
                 //给 lable 添加手势
                 lable.isUserInteractionEnabled = true
                 let tapGes = UITapGestureRecognizer(target: self, action: #selector(self.titleLableClick(tapGes:)))
                 lable.addGestureRecognizer(tapGes)
                 
             }
             
         }
         //设置底线和滚动滑块
         private func setupBottomMenuAndScrollLine(){
             //1,添加底线
             let bottomLine = UIView()
             bottomLine.backgroundColor = UIColor.lightGray
             let lineH : CGFloat = 0.5
             bottomLine.frame = CGRect(x:0,y:frame.height-lineH,width: frame.width,height: lineH)
             addSubview(bottomLine)
             //获取第一个lable
             guard  let firstLable = titleLables.first else {return}
             firstLable.textColor = UIColor(r: kSelectColor.0, g: kSelectColor.1, b: kSelectColor.2)
             //设置
             scrollView.addSubview(scrollLine)
             //设置scrollView 属性
             scrollLine.frame = CGRect(x: firstLable.frame.origin.x, y: frame.height-kScrollLineH, width: firstLable.frame.width, height: kScrollLineH)
         }
         
        
 }

extension PageTitleView {
    @objc private func titleLableClick(tapGes : UITapGestureRecognizer){
        //1,获取当前lable
         guard   let currentLable = tapGes.view as? UILabel else {return}
        //获取之前的lable
        let oldLable = titleLables[currentIndex]
       //切换文字的颜色
        currentLable.textColor = UIColor(r: kSelectColor.0, g: kSelectColor.1, b: kSelectColor.2)
        oldLable.textColor = UIColor(r: kNormalColor.0, g: kNormalColor.1, b: kNormalColor.2)
        //保存最新的label的下标值
        currentIndex = currentLable.tag
        
        //滚动条文职发生改变
        let scrollLineX = CGFloat(currentLable.tag) * scrollLine.frame.width
        UIView.animate(withDuration: (0.15)) {
            self.scrollLine.frame.origin.x = scrollLineX
        }
        
        //通知代理 初始化代理数据
        //代理里面都干了啥 把我的什么属性传出去
        delegate?.pageTitleVeiw(titleView:self,selectedIndex: currentIndex)
        
    }
}
//对外暴露的方法
extension PageTitleView {
    func setTitleWithProgress(progress :CGFloat,sourceIndex :Int,targetIndex :Int  ) {
         //取出数据做处理 //取出 sourceLable /targetLable
        let sourceLable = titleLables[sourceIndex]
        let targetLable = titleLables[targetIndex]
        
        //处理滑块的逻辑
       let moveTotalX = targetLable.frame.origin.x - sourceLable.frame.origin.x
       let moveX = moveTotalX * progress
        scrollLine.frame.origin.x = sourceLable.frame.origin.x + moveX
      //颜色渐变
        //取出变化的范围
        let colorDelta = (kSelectColor.0 - kNormalColor.0,kSelectColor.1 - kNormalColor.1,kSelectColor.2 - kNormalColor.2)
        //变化 sourceLable
        sourceLable.textColor = UIColor(r: kSelectColor.0 - colorDelta.0 * progress, g: kSelectColor.1 - colorDelta.1 * progress, b: kSelectColor.2 - colorDelta.2 * progress)
        //变化targetLable
        targetLable.textColor = UIColor(r: kNormalColor.0 + colorDelta.0 * progress, g: kNormalColor.1 + colorDelta.1 * progress, b: kNormalColor.2 + colorDelta.2 * progress)
        //记录最新的index
         currentIndex = targetIndex
        
        
    }
}
